/**
 * @param {number[]} nums
 * @return {number[][]}
 */
var subsetsWithDup = function(nums) {
  nums = nums.sort((a, b) => (a - b));
  let result = [], tempArr = [], used = new Array(nums.length).fill(false);
  function dfs(begin) {
    result.push([...tempArr]);
    for(let i = begin; i < nums.length; i++) {
      if(i > begin && !used[i] && nums[i] === nums[i - 1]) continue;
      tempArr.push(nums[i]);
      used[i] = true;
      dfs(i + 1);
      used[i] = false;
      tempArr.splice(tempArr.length - 1, 1);
    }
  }

  dfs(0);
  return result;
};
let nums = [1,2,2];
console.log(subsetsWithDup(nums));
nums = [0];
console.log(subsetsWithDup(nums));
